package com.bins.langchain.langchain4j.controller;

import com.bins.langchain.langchain4j.service.IMilvusService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/vector")
public class MilvusController {

    @Autowired
    private IMilvusService milvusService;

    @GetMapping(value = "/hasCollectionName")
    public Boolean isExistCollectionName(@RequestParam(value = "collectionName") String collectionName) {
        return milvusService.hasCollect(collectionName);
    }

    @PostMapping(value = "/createCollection")
    public void createCollection(@RequestParam(value = "collectionName") String collectionName,
                                 @RequestParam(value = "collectionDesc") String collectionDesc) {
        milvusService.create(collectionName, collectionDesc);
    }

    @PostMapping(value = "/insertData")
    public Boolean insertData(@RequestParam(value = "collectionName") String collectionName,
                              @RequestParam(value = "textId") Integer textId) {
        List<List<Float>> vector = Lists.newArrayList();
        List<Long> textIds = Lists.newArrayList();
        for (int i = 0; i < textId; i++) {
            textIds.add(Long.valueOf(i));
            List<Float> itemList = Lists.newArrayList();
            for (int j = 0; j < textId; j++) {
                itemList.add(new Float(i));
            }
            vector.add(itemList);
        }
        return milvusService.insert(collectionName, textIds, vector);
    }
}
